package com.itheima.web.controller;


import com.github.pagehelper.PageInfo;
import com.itheima.domain.Permission;
import com.itheima.domain.Role;
import com.itheima.domain.UserInfo;
import com.itheima.utils.BeanUtil;
import com.itheima.utils.MD5Util;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/user")
public class UserServlet extends BaseServlet {

    /**
     * 分页查询
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            //获取要搜索的值
            int page = 1;
            int size = 5;
            if (StringUtils.isNotBlank(request.getParameter("pageNum"))) {
                page = Integer.parseInt(request.getParameter("pageNum"));
            }
            if (StringUtils.isNotBlank(request.getParameter("pageSize"))) {
                size = Integer.parseInt(request.getParameter("pageSize"));
            }

            //获取当前参数

            String username = request.getParameter("byUsername");
            PageInfo<UserInfo> all = userService.findAll(page, size, username);
            request.setAttribute("page", all);
            request.getRequestDispatcher("/WEB-INF/pages/user/user-list.jsp").forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 访问添加页面
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void toAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //请求转发到增加页面
        request.getRequestDispatcher("/WEB-INF/pages/user/user-add.jsp").forward(request, response);
    }

    /**
     * 保存
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        UserInfo userInfo = BeanUtil.fillBean(request, UserInfo.class);
        userService.save(userInfo);
        request.getRequestDispatcher("/user?operation=list").forward(request, response);
    }

    /**
     * 批量删除
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void deleteAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String idsStr = request.getParameter("ids");
        String[] ids = idsStr.split(",");
        userService.deleteAll(ids);
        list(request, response);
    }

    /**
     * 根据id查找
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void findById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        UserInfo user = userService.findById(id);
        request.setAttribute("user", user);
        request.getRequestDispatcher("/WEB-INF/pages/user/user-show.jsp").forward(request, response);
    }

    /**
     * 修改状态
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void updateStatus(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        userService.updateStatus(id);
        list(request, response);
    }

    /**
     * 访问更改角色页面
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void toEditRole(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //用户的id
        String userId = request.getParameter("id");
        request.setAttribute("id", userId);
        List<Role> roles = roleService.findAllRoleByUserId(userId);
        request.setAttribute("roles", roles);
        request.getRequestDispatcher("/WEB-INF/pages/user/user-role-add.jsp").forward(request, response);
    }

    public void edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("userId");
        String[] ids = request.getParameterValues("ids");
        userService.updateRole(id, ids);
        list(request, response);
    }

    public void login(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String username = request.getParameter("username");
//        String password = MD5Util.md5(request.getParameter("password"));
        String password = request.getParameter("password");
        HttpSession session = request.getSession();
        //
        if (username == null || username == "" || password == null || password == "") {
            request.setAttribute("data", "请输入用户名和密码");
            request.getRequestDispatcher("/WEB-INF/pages/login.jsp").forward(request, response);
        }

        //进行验证
        UserInfo userInfo = userService.login(username, password);
        if (userInfo != null) {
            session.setAttribute("userInfo", userInfo);


            //如果登录成功，加载对应的角色对应的模块
            List<Permission> permissions = userService.findPermissionByUserId(userInfo.getId());
            request.getSession().setAttribute("permissionList",permissions);



            request.getSession().setAttribute("loginUser", userInfo);
            request.getRequestDispatcher("/WEB-INF/pages/common/main.jsp").forward(request, response);
        } else {
            request.setAttribute("data", "用户名或密码错误");
            request.getRequestDispatcher("/WEB-INF/pages/login.jsp").forward(request, response);
        }
    }
}